package sharetime.playlist.controllers;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sharetime.playlist.domain.Playlist;
import sharetime.playlist.domain.PlaylistResource;
import sharetime.playlist.service.IPlaylistManager;
import sharetime.playlist.service.IPlaylistResourceManager;
import sharetime.web.controllers.BaseController;

/**
 * 
 * @author zhangyf
 * 
 */
@Controller
@RequestMapping("/playlistresource")
public class EditPlaylistResourceController extends BaseController {

	@Autowired
	@Qualifier("playlistManager")
	private IPlaylistManager playlistManager;

	@Autowired
	@Qualifier("playlistResourceManager")
	private IPlaylistResourceManager playlistResourceManager;

	@RequestMapping(method = RequestMethod.GET)
	public String index(@RequestParam
	long id, Model model) {
		return null;
	}

	@RequestMapping(method = RequestMethod.POST, params = "!_method")
	public void create(@RequestParam long playlistId, @ModelAttribute PlaylistResource pr, 
						BindingResult br, HttpServletResponse response, Model model) {
		if (br.hasErrors()) {
			// 将结果返回(json格式)，显示于form上
		} else {
			Playlist pl = playlistManager.getPlaylistById(playlistId);
			pr.setPlaylist(pl);
			playlistResourceManager.createPlaylistResource(pr);
			
			String[] excludeAttributes = { "*.playlist" , "*.owner"};
			String[] includeAttributes = {};

			model.addAttribute("msg", "OK");

			renderJson(response, model, excludeAttributes, includeAttributes);
		}
	}

	@RequestMapping(method = RequestMethod.POST, params = "_method=PUT")
	public String update() {
		return null;
	}

	@RequestMapping(method = RequestMethod.POST, params = "_method=DELETE")
	public String remove() {
		return null;
	}
}
